var pub_ns:NetStream;
var sub_ns:NetStream;

var attached_pub_ns:NetStream;
// var attached_sub_ns:NetStream;

// var attachedStream:String;

var gain:Number=50;
var rate:Number=8;

var micInterval:Number;
var TIMESLOT:Number=200;

function setGain(param:Number)
{
	gain=param;
}

function setRate(param:Number)
{
	rate=param;
}

function publishAttached(uri:String)
{
	if (attached_pub_ns==null)
	{
		var nc=Packages.services.RTMPConn.getInstance();
		if (nc.connected)
		{
			attached_pub_ns=new NetStream(nc);
		}
		else return;
	}

	var mic=initMicSettings();
	attached_pub_ns.attachAudio(mic);
	attached_pub_ns.publish(uri+"_attached", "live");

	// dispatch collab event - highlight speakerHolder
	Packages.modula.task.Common.highlightSpeakerHolder(true);
}

function unpublishAttached()
{
	if (attached_pub_ns==null)
	{
		return;
	}
	else 
	{	
		attached_pub_ns.publish(false);
		attached_pub_ns.close();
	}

	// dispatch collab event - no highlight speakerHolder
	Packages.modula.task.Common.highlightSpeakerHolder(false);
}

function initMicSettings():Microphone
{
	var active_mic:Microphone=Microphone.get();
	if (gain!=null) active_mic.setGain(gain);
	if (rate!=null) active_mic.setRate(rate);
	var silenceLevel;
	if (active_mic.gain<=50)
	{
		silenceLevel=10;
	}
	else if (active_mic.gain<=80)
	{
		silenceLevel=20;
	}
	else if (active_mic.gain<=100)
	{
		silenceLevel=30;
	}
	active_mic.setSilenceLevel(silenceLevel, 2000);

	return active_mic;
}

/** 2008-11-28
	attached stream used by communication
*/
function publish(uri:String, type:String, attached:Boolean)
{
	Packages.log(Packages.model.UserProfile.data.nickName+".publish: "+uri, "info");
	
	// initialize the mic settings
	var mic=initMicSettings();

	// Packages.log("publish: "+uri+" "+gain+" "+rate+" "+silenceLevel, "debug");
	
	if (pub_ns==null)
	{
		var nc=Packages.services.RTMPConn.getInstance();
		if (nc.connected)
		{
			pub_ns=new NetStream(nc);
		}
		else return;
	}
	// else pub_ns.publish(false);

	pub_ns.attachAudio(mic);
	pub_ns.publish(uri, type);

	if (attached)
	{
		publishAttached(abstractStreamName(uri));
	}

	activity();
}

function activity()
{
	if (micIntervalId!=null) clearInterval(micIntervalId);
	micIntervalId=setInterval(onActivity, TIMESLOT);

	active_mic.onActivity=function(active:Boolean) 
	{
		// active or silence
		Packages.system.mlf.GateKeeper.dispatchMessage({type:"onMicStatus", param:active?"active":"silence"});
	}
}

function onActivity()
{
	var active_mic:Microphone=Microphone.get();
	// Packages.log("onActivity: "+active_mic.activityLevel, "debug");
	Packages.system.mlf.GateKeeper.dispatchMessage({type:"onLadderChange", param:active_mic.activityLevel});
}

function subscribe(uri:String, attached:Boolean)
{
	Packages.log(Packages.model.UserProfile.data.nickName+".subscribe: "+uri, "info");
	// var uriArr=uri.split("#");
	if (sub_ns==null)
	{
		var nc=Packages.services.RTMPConn.getInstance();
		if (nc.connected)
		{
			sub_ns=new NetStream(nc);
		}
		else return;
	}
	
	// set Buffer Time
	flv_ns.setBufferTime(0);

	if (attached)
	{
		sub_ns.play(uri+"_attached");
	}
	else sub_ns.play(uri);
	// sub_ns.play(uriArr[0]);

	/* if (nsInterval!=null) clearInterval(nsInterval); */
}

function unsubscribe()
{
	Packages.log(Packages.model.UserProfile.data.nickName+".unsubscribe", "info");
	if (sub_ns==null)
	{
		return;
	}
	else sub_ns.play(false);
}

function resume(uri:String, attached:Boolean)
{
	// pub_ns.publish(false);
	Packages.log(Packages.model.UserProfile.data.nickName+".resume: "+uri, "info");
	if (pub_ns==null)
	{
		publish(uri, "append", attached);
	}
	else
	{
		pub_ns.publish(uri, "append");
		if (attached) attached_pub_ns.publish(abstractStreamName(uri)+"_attached", "live");
		activity();
	}
}

function unpublish(attached:Boolean)
{
	Packages.log(Packages.model.UserProfile.data.nickName+".unpublish", "info");
	if (pub_ns==null)
	{
		return;
	}
	else 
	{	
		pub_ns.publish(false);
		pub_ns.close();
	}

	if (attached)
	{
		unpublishAttached();
	}

	if (micIntervalId!=null) clearInterval(micIntervalId);
}

function close()
{
	Packages.log(Packages.model.UserProfile.data.nickName+".close", "info");
	
	pub_ns.publish(false);
	pub_ns.close();
	sub_ns.close();
	
	if (micIntervalId!=null) clearInterval(micIntervalId);
	// if (nsInterval!=null) clearInterval(nsInterval);
}

function saveMicSettings()
{
	var obj:Object=new Object();
	obj.gain=gain;
	obj.rate=rate;
	Packages.modula.task.Composition.micSettings=obj;
}

function loadMicSettings()
{
	var obj:Object=Packages.modula.task.Composition.micSettings;
	if (obj!=null)
	{
		gain=obj.gain;
		rate=obj.rate;
	}
}

function abstractStreamName(uri:String):String
{
	var streamName:String=uri;
	if (uri.indexOf("#")>-1)
	{
		streamName=uri.substr(0, uri.indexOf("#"));
	}
	// attachedStream=streamName;
	return streamName;
}